﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Bentley.CIMNET.CIMGeometry.Mesh;
using GeneralHelpers = CheccCoverplateCulvertAssemble.Tools.GeneralBank;

namespace Checc_CircularCulvert_Assemble.commons
{
    public class DrawHelper
    {
        /// <summary>
        /// 创建复杂面
        /// </summary>
        /// <param name="curveCurveDown"></param>
        /// <param name="curveCurveTop"></param>
        /// <returns></returns>
        public static PolyfaceHeader CreatePolyfaceHeader(LineStringElement curveCurveDown, LineStringElement curveCurveTop)
        {
            PolyfaceHeader polyfaceHeader = null;
            MeshSDKNET.CreateMeshFromTwoCurves(ref polyfaceHeader, curveCurveDown.GetCurveVector(), curveCurveTop.GetCurveVector(), GeneralHelpers.Default3DModel);
            return polyfaceHeader;
        }

        /// <summary>
        /// 创建规则面
        /// </summary>
        /// <param name="downDPoint3Ds"></param>
        /// <param name="topDPoint3Ds"></param>
        /// <returns></returns>
        public static PolyfaceHeader CreatePolyfaceHeader(List<DPoint3d> downDPoint3Ds, List<DPoint3d> topDPoint3Ds)
        {
            if (topDPoint3Ds.Count == 0)
            {
                List<DPoint3d> dPoint3DTops = new();
                dPoint3DTops.AddRange(downDPoint3Ds);

                List<DPoint3d> dPoint3DTopDowns = new();
                dPoint3DTopDowns.AddRange(topDPoint3Ds);

                dPoint3DTopDowns.Reverse();
                dPoint3DTops.AddRange(dPoint3DTopDowns);

                List<int> indices = new();
                PolyfaceHeader polyHeader = new()
                {
                    Point = dPoint3DTops
                };
                for (int i = 1; i <= dPoint3DTops.Count; i++)
                {
                    indices.Add(i);
                }
                polyHeader.AddIndexedFacet(indices, null, null, indices);
                MeshHeaderElement meshHeaderElement = new(GeneralHelpers.Default3DModel, null, polyHeader);
                return polyHeader;
            }
            else
            {
                LineStringElement lineStringElement = new(GeneralHelpers.Default3DModel, null, downDPoint3Ds.ToArray());
                List<DPoint3d> dPoint3DTops = new();
                dPoint3DTops.AddRange(topDPoint3Ds);
                dPoint3DTops.Reverse();
                LineStringElement lineStringElement2 = new(GeneralHelpers.Default3DModel, null, dPoint3DTops.ToArray());
                return CreatePolyfaceHeader(lineStringElement, lineStringElement2);
            }

        }

        /// <summary>
        /// 缝合面
        /// </summary>
        /// <param name="polyfaceHeaders"></param>
        /// <returns></returns>
        public static MeshHeaderElement CraeteMeshHeaderElement(List<PolyfaceHeader> polyfaceHeaders)
        {
            PolyfaceConstruction polyCon = new PolyfaceConstruction();

            foreach (PolyfaceHeader item in polyfaceHeaders)
            {
                polyCon.Add(item);
            }
            PolyfaceHeader polyHeader = polyCon.GetClientMesh();
            MeshHeaderElement meshResult = new MeshHeaderElement(GeneralHelpers.Default3DModel, null, polyHeader);
            return meshResult;
        }

    }
}
